﻿/*----------------------------------------------------------------
 * 
 * 版权所有：Copyright (C) 2013
 * 
 * 文件名：FILE_NAME
 * 文件功能描述：
 * 
 * 
 * 作 者：zhangq
 * 邮 箱：ifunnyface@outlook.com
 * 日 期：2013-01-07 14:49:16
 * 
 * 
 * 修改标识： Author
 * 修改描述： 相关描述
 * 
 * ---------------------------------------------------------------- */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Transactions;
using Common.Extensions.OrderBy;
using Common.Extensions.Where;
using DomainModel;
using IDataAccess;
using IManager;

namespace Manager
{

    /// <summary>
    /// 
    /// </summary>
    public class ManagerArticle : IManagerArticle
    {

        /// <summary>
        /// 用户信息数据访问接口
        /// </summary>
        public IDataAccessPartition<ArticlePartitionInfo> IDAPartition { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public ManagerArticle()
        {
            // TODO:
        }

        /// <summary>
        /// 通过Id获取分类
        /// </summary>
        /// <param name="partitionId">Id</param>
        /// <returns>分类信息</returns>
        public ArticlePartitionInfo GetPartition(string partitionId)
        {
            return IDAPartition.Find(partitionId);
        }


        /// <summary>
        /// 获取最大编号
        /// </summary>
        /// <param name="prefix">前缀</param>
        /// <returns>编号</returns>
        public string Max(string prefix)
        {
            var p = PredicateExtension.True<ArticlePartitionInfo>();
            p = p.And(e => e.PartitionId.StartsWith(prefix));
            return IDAPartition.Filter(p).Max(e => e.PartitionId.Substring(prefix.Length));
        }


        /// <summary>
        /// 获取分类
        /// </summary>
        /// <returns>分类列表</returns>
        public IList<ArticlePartitionInfo> GetPartitions()
        {
            return IDAPartition.LoadAll().ToList();
        }


        /// <summary>
        /// 添加分类信息
        /// </summary>
        /// <param name="partitionInfo">分类信息</param>
        /// <returns>True=操作成功/False=操作失败</returns>
        public bool AddPartition(ArticlePartitionInfo partitionInfo)
        {
            const int newId = 1;
            var pid = partitionInfo.ParentId;
            var p = IDAPartition.Find(pid);
            var id = Max(pid);
            if (string.IsNullOrEmpty(id))
            {
                id = Convert.ToString(newId).PadLeft(2, '0');
            }
            else
            {
                id = Convert.ToString(Convert.ToInt32(id) + newId).PadLeft(2, '0');
            }
            partitionInfo.Level = p.Level + 1;
            partitionInfo.PartitionId = (pid + id);
            IDAPartition.Create(partitionInfo);
            p.HasChildren = 1;
            IDAPartition.Update(p);
            IDAPartition.UnitOfWork.Commit();
            return true;
        }


        /// <summary>
        /// 删除分类信息
        /// </summary>
        /// <param name="partitionId">Id</param>
        /// <returns>True=操作成功/False=操作失败</returns>
        public bool DeletePartition(string partitionId)
        {
            IDAPartition.Delete(partitionId);
            IDAPartition.UnitOfWork.Commit();
            return true;
        }


        /// <summary>
        /// 删除分类信息
        /// </summary>
        /// <param name="partitionIds">Id</param>
        /// <returns>True=操作成功/False=操作失败</returns>
        public bool DeletePartition(IList<string> partitionIds)
        {
            foreach (var partitionId in partitionIds)
            {
                var p = GetPartition(partitionId);
                if (p != null && p.Level > 0)
                {
                    var parent = p.ArticlePartition;
                    IDAPartition.Delete(partitionId);
                    if (parent.Children == null || parent.Children.Count == 0)
                    {
                        parent.HasChildren = 0;
                        IDAPartition.Update(parent);
                    }
                }
            }
            IDAPartition.UnitOfWork.Commit();
            return true;
        }



        /// <summary>
        /// 更新分类信息
        /// </summary>
        /// <param name="partitionInfo">分类信息</param>
        /// <returns>True=操作成功/False=操作失败</returns>
        public bool UpdatePartition(ArticlePartitionInfo partitionInfo)
        {
            IDAPartition.Update(partitionInfo);
            IDAPartition.UnitOfWork.Commit();
            return true;
        }


        /// <summary>
        /// 根据条件分页获得记录
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="orderByExpressions">排序字段</param>
        /// <returns>记录列表</returns>
        public IList<ArticlePartitionInfo> LoadAll(Expression<Func<ArticlePartitionInfo, bool>> where,
                                                   int pageIndex,
                                                   int pageSize,
                                                   params IOrderByExpression<ArticlePartitionInfo>[] orderByExpressions)
        {
            return IDAPartition.Filter(where, pageIndex, pageSize, orderByExpressions).ToList();
        }


        /// <summary>
        /// 获取总数
        /// </summary>
        /// <param name="where">条件</param>
        /// <returns>记录总数</returns>
        public int LoadCount(Expression<Func<ArticlePartitionInfo, bool>> where)
        {
            return IDAPartition.Count(where);
        }

    }

}